<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  class product_category extends CI_Controller{
      protected $_templates;
      function __construct() {
		parent::__construct();
		$this->pre_message = "";   
		$this->session->set_userdata(array('Url'=>uri_string()));
		$this->load->library('string_library');
		$this->load->helper('upload_file');
		$this->load->library('image_library');
		$this->load->model('product_category_model','product_category');
      }   
	  function index(){
		  $this->listproduct_category();
	  }
      function listproduct_category($action=''){
		 // check quyền xem
         $this->acl->check('view','mod_product_category','product_category',base_url());
        /*Begin search*/
            $type = $this->input->post('type');
			
			if($_POST){
				$name = $this->input->post('name');
				$tinhtrang = $this->input->post('tinhtrang');
				$data['arr_search'] = array('name'=>$name,'tinhtrang'=>$tinhtrang);
			}else $data['arr_search']='';
			
		
            $data['title'] = 'Danh sách sản phẩm';
			if($this->acl->check('add','mod_product_category','product_category'))
            $data['add'] = 'product_category/add';
            $config['base_url'] = base_url().'product_category/listproduct_category/'.(int)$this->uri->segment(3);  
            $data['num'] = $this->product_category->get_num_data($data['arr_search']);
           	$config['total_rows']   =  $data['num'];
         	$config['per_page']  =   '20';
           	$config['uri_segment'] = 4;   
            $this->pagination->initialize($config);
            $data['list'] =   $this->product_category->get_all_data($config['per_page'],(int)$this->uri->segment('4'),$data['arr_search']);
			$data['pagination']    ='';/* $this->pagination->create_links();  */     
            $this->_templates['page'] = 'product_category/listproduct_category';
           if($type=="ajax")
                echo $this->load->view($this->_templates['page'],$data,true);
            else
                    $this->site_lib->load($this->_templates['page'],$data); 
      }
    
      function add(){ 
	  		// check quyền thêm
	  		//$this->acl->check('add','mod_product_category','product_category',base_url());                        
			$data['title'] = 'Thêm danh mục sản phẩm';
			$data['option_pid'] = $this->product_category->optionCategoryLevel();
			$this->form_validation->set_rules('name',"Tên danh mục sản phẩm",'trim|required');
             if( $this->form_validation->run() ){
				 // lưu alias các cấp danh mục sản phẩm
				$parent_id = $this->input->post('parent_id');
				$procate_name_alias = $this->product_category->getAlias($parent_id);
				if($procate_name_alias)
					$name_alias =$procate_name_alias.'/'. $this->string_library->stringchange($this->input->post('name'));
				else
					$name_alias =$this->string_library->stringchange($this->input->post('name'));
				$image = $this->input->post('image');	
				$arr_alias = array('name_alias'=>$name_alias); 
				//upload hình đại diện
				if($image!=''){
				 $dirFile = "../uploads/images/";
				 $dirFileAvatar = "../uploads/productcate/";
				 $filename =$_POST['image'];
				 $dirFileRoot =$dirFile.$filename;
				 $dirFileResizeSmall = $dirFileAvatar.$filename;
				 $img_name =$this->image_library->resizeThumbnailImage($dirFileResizeSmall,$dirFileRoot,320,370,0,0,0);
				 $filename==''? $img_name='':$img_name=$filename;
				 $arr_image = array('image'=>$filename); 
				}else{
				 $arr_image = array('image'=>'');
				}
				$data = $this->build_data($_POST,1);
				$data  = array_merge($data,$arr_alias);
				$data  = array_merge($data,$arr_image);
				 if($this->product_category->saveData(0,$data)){
					 $this->session->set_flashdata('message','Lưu thành công');
					 redirect('product_category/listproduct_category');
				 }else {
					 $this->pre_message = 'Lưu không thành công';
				 }
				
             }else if(!empty($_POST)){
				  
				 $this->pre_message = validation_errors();	 
             }
          $data['message'] = $this->pre_message;
          $this->_templates['page'] = 'product_category/add';
          $this->site_lib->load($this->_templates['page'],$data);
      }
       function edit($id){    
	  		 // check quyền thêm 
	  		//$this->acl->check('edit','mod_product_category','product_category',base_url());                              
            $data['title'] = 'Sửa bài viết';
            $data['query'] = $this->product_category->get_object($id);
            $data['option_pid'] = $this->product_category->optionCategoryLevel(0,$data['query']->parent_id);
          	$this->form_validation->set_rules('name',"Tên danh mục sản phẩm",'trim|required');
             if($this->form_validation->run() == FALSE ){
				
                $this->pre_message .= validation_errors();
             }else{
				
				 // lưu alias các cấp danh mục sản phẩm
				$parent_id = $this->input->post('parent_id');
				$procate_name_alias = $this->product_category->getAlias($parent_id);
				if($procate_name_alias)
					$name_alias =$procate_name_alias.'/'. $this->string_library->stringchange($this->input->post('name'));
				else
					$name_alias =$this->string_library->stringchange($this->input->post('name'));
				$image = $this->input->post('image');
				$arr_alias = array('name_alias'=>$name_alias); 
				if($image!=''){
				 $dirFile = "../uploads/images/";
				 $dirFileAvatar = "../uploads/productcate/";
				 $filename =$_POST['image'];
				 $dirFileRoot =$dirFile.$filename;
				 $dirFileResizeSmall = $dirFileAvatar.$filename;
				 $img_name =$this->image_library->resizeThumbnailImage($dirFileResizeSmall,$dirFileRoot,320,370,0,0,0);
				 $filename==''? $img_name='':$img_name=$filename;
				 $arr_image = array('image'=>$filename); 
				}else{
					$image_old =$data['query']->image;
				 	$arr_image = array('image'=>$image_old);
				}			
				$data = $this->build_data($_POST,1);
				$data  = array_merge($data,$arr_alias);
				$data  = array_merge($data,$arr_image);
				 if($this->product_category->saveData($id,$data)){
					 $this->session->set_flashdata('message','Lưu thành công');
					 redirect('product_category/listproduct_category');
				 }else{
					 $this->pre_message = 'Lưu không thành công';
				}
		 }
          $data['message'] = $this->pre_message;
          $this->_templates['page'] = 'product_category/edit';
          $this->site_lib->load($this->_templates['page'],$data);
      }
      function del(){
	   // check quyền xóa
		//$this->acl->check('del','mod_product_category','product_category',base_url());   
        $id = $this->uri->segment(3);
        $page = $this->uri->segment(4);
        if($this->product_category->delete($id))
            $this->session->set_flashdata('message','Đã xóa thành công');
        else $this->session->set_flashdata('message','Xóa không thành công');
        
        redirect('product_category/listproduct_category/'.$page);
    }
    function dels(){
		// check quyền xóa nhiều 
		//$this->acl->check('dels','mod_product_category','product_category',base_url());   
        if(!empty($_POST['ar_id']))
        {
            $page = (int)$this->input->post('page');
            $ar_id = $this->input->post('ar_id');

            if(!empty($_POST['btn_submit']))
            {
                for($i = 0; $i < sizeof($ar_id); $i ++) {
                    if ($ar_id[$i]){
                        if($this->product_category->delete($ar_id[$i]))
                            $this->session->set_flashdata('message','Đã xóa thành công');
                        else $this->session->set_flashdata('message','Xóa không thành công');
                    }
                }
            }
        }
        redirect('product_category/listproduct_category/'.$page);
    }
    function build_data($data,$flag=1)
	{//flag=1:0?insert:update
				$dataUpdate["parent_id"]=trim($data["parent_id"]);
				$dataUpdate["name"]=trim($data["name"]);
				$dataUpdate["home"]=trim($data["home"]);
				$dataUpdate["nb_order"]=trim($data["nb_order"]);
				if($flag==1){
               		$dataUpdate["dt_create"] = date('Y-m-d H:i:s');
				}
                if(isset($data["bl_active"])){
					$dataUpdate["bl_active"]=trim($data["bl_active"]);
				}else{
					$dataUpdate["bl_active"]=0;
				}
		return $dataUpdate;
	}
	

  }
?>
